package com.yan.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpRequest;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;

import com.alibaba.fastjson.JSON;
import com.yan.bean.Blog;
import com.yan.bean.Link;
import com.yan.config.RedisConfig;
import com.yan.mapper.LinkMapper;
import com.yan.requestLimit.RequestLimit;
import com.yan.service.TryableService;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/index")
@EnableRetry
public class IndexController {

	Logger logger = Logger.getLogger(IndexController.class);
	
	@Autowired
	private TryableService tryableService;
	int totalNum=1000;
	
	@RequestMapping(value = "/indexs", method = { RequestMethod.POST, RequestMethod.GET })
	// @RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})//分模块的请求名字
	@ResponseBody
	public Map<String, String> getData(HttpServletRequest request, Blog blog) {

		System.out.println("blog======" + blog + request.getParameter("jsonData") + " "
				+ JSON.parseObject(request.getParameter("jsonData"), Blog.class));

		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "an");
		map.put("sex", "男男男");
		return map;
	}

	@RequestMapping("/index")
	@ResponseBody
	public String index() {
		return "index";
	}

	@RequestLimit(count = 10, time = 10000)
	@RequestMapping("/indexJsp")
	public String indexJsp(HttpServletRequest request) {
		return "index"; // 没有用ResponseBody，返回string，跳转的是jsp页面："login" + ".jsp"后缀
	}

	@Autowired
	LinkMapper linkMapper;

	@RequestMapping(value = "/link", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String linkInfo() {
		Link link = linkMapper.find();
		return link.getLinkName() + " " + link.getLinkUrl();
	}

	@RequestLimit(count = 10, time = 10000)
	@RequestMapping(value = "/limit")
	@ResponseBody
	public String limitCount(HttpServletRequest request) {
		Link link = linkMapper.find();
		return link.getLinkName() + " " + link.getLinkUrl();
	}

	@ApiOperation(value = "spring重试方法")
	@RequestMapping(value = "/try")
	@ResponseBody
	public String trys(HttpServletRequest request) {
		try {
			tryableService.call();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "try...";

	}

	@RequestMapping(value = "/chinese")
	@ResponseBody
	public String chinese() {
		return "中文测试";
	}

	@RequestMapping(value = "/sessiontest", method = RequestMethod.GET)
	public Map<String, Object> firstResp(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		request.getSession().setAttribute("request Url", request.getRequestURL());
		map.put("request Url", request.getRequestURL());
		return map;
	}

	@RequestMapping(value = "/getsessions", method = RequestMethod.GET)
	public Object sessions(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		map.put("sessionId", request.getSession().getId());
		map.put("message", request.getSession().getAttribute("map"));
		return map;
	}

	@RequestMapping(value = "/retry", method = RequestMethod.GET)
	@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
	@ResponseBody
    public int minGoodsnum(int num) throws Exception{
        logger.info("minGoodsnum开始"+ System.currentTimeMillis());
        if(num <= 10){
            throw new Exception("数量不对");
        }
        logger.info("minGoodsnum执行结束");
        return totalNum - num;
    }
}
